
#############################################################################################
################## 项目属性设置 ##############################################################
#############################################################################################

cmake_minimum_required(VERSION 2.8)
project(MyFavoriteProj)



#############################################################################################
################## 项目参数，函数定义 #########################################################
#############################################################################################

set(FLD_3RDPARTY      "3rdparty")
set(FLD_3RDPARTY_INC  "include")
set(FLD_3RDPARTY_LIB  "lib")
set(FLD_BUILD         "build")
set(FLD_DEPLOY        "deploy")
set(FLD_DOC           "doc")
set(FLD_SRC           "src")
set(FLD_CONFIGURATION _configuration)
set(FLD_RESOURCES     _resources)
set(FLD_DATA          _data)

set(FIL_EXTRA         CMakeConf.txt)
set(FIL_LIB_PATH      setlibpath.sh)

set(SRCS "")
set(TAR  "")
set(BLD  "")
set(PTH  "")
set(CPY  "")

function(clear_var arg0_to_clear)
    set(${arg0_to_clear} "" PARENT_SCOPE)
endfunction()

function(as_lib_path_script_str arg_path arg_oldroot arg_newroot arg_oldlib arg_newlib arg_out)
    set(LCL_STR ${arg_path})
    string(REPLACE ${arg_oldroot}  ${arg_newroot} LCL_STR ${LCL_STR})
    string(REPLACE ${arg_oldlib}   ${arg_newlib}  LCL_STR ${LCL_STR})
    set(LCL_STR "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${LCL_STR}")
    set(${arg_out} ${LCL_STR} PARENT_SCOPE)
endfunction()

function(as_copy_path arg_list arg_oldlib arg_out)
    set(LCL_STR ${arg_list})
    string(REPLACE ${arg_oldlib} ";" LCL_STR ${LCL_STR})
    set(${arg_out} ${LCL_STR} PARENT_SCOPE)
endfunction()

function(use_path_as_tar arg0_cwd arg1_out)
    set(LCL_TAR ${arg0_cwd})
    string(REPLACE " "  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "."  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "-"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "/"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "\\" "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "+"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "~"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "!"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "@"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "#"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "$"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "%"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "^"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "&"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "&"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "*"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "("  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE ")"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "="  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "["  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "{"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "]"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "}"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "|"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE ";"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE ":"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "'"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "\"" "_" LCL_TAR ${LCL_TAR})
    string(REPLACE ","  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "<"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "."  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE ">"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "?"  "_" LCL_TAR ${LCL_TAR})
    string(REPLACE "`"  "_" LCL_TAR ${LCL_TAR})
    set(${arg1_out} ${LCL_TAR} PARENT_SCOPE)
endfunction()



#############################################################################################
################## 项目编译 ##################################################################
#############################################################################################
# //编译目录选项
set(DIR_3RDPARTY_INC "")# 第三方库头文件目录
set(DIR_3RDPARTY_LIB "")# 第三方库库文件目录
set(FIL_3RDPARTY_LIB "")# 第三方库库文件

file(GLOB DIR_3RDPARTY_INC ${FLD_3RDPARTY}/*/${FLD_3RDPARTY_INC})
file(GLOB DIR_3RDPARTY_LIB ${FLD_3RDPARTY}/*/${FLD_3RDPARTY_LIB})
file(GLOB FIL_3RDPARTY_LIB ${FLD_3RDPARTY}/*/${FLD_3RDPARTY_LIB}/*.so ${FLD_3RDPARTY}/*/${FLD_3RDPARTY_LIB}/*.a)# ${FLD_3RDPARTY}/*/${FLD_3RDPARTY_LIB}/*.la

# //额外库的头文件及库文件，编译选项
include(${CMAKE_CURRENT_SOURCE_DIR}/${FIL_EXTRA})

# //LD_LIBRARY_PATH，库路径脚本
string(TOLOWER ${CMAKE_BUILD_TYPE} BLD)
if(DIR_3RDPARTY_LIB)
    as_lib_path_script_str("${DIR_3RDPARTY_LIB}" "${CMAKE_CURRENT_SOURCE_DIR}/${FLD_3RDPARTY}" "`pwd`" "//${FLD_3RDPARTY_LIB}" "/${FLD_3RDPARTY_LIB}:" PTH)
endif()
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_BUILD}/${BLD}/${FIL_LIB_PATH} ${PTH})

# //复制库
if(DIR_3RDPARTY_LIB)
    as_copy_path("${DIR_3RDPARTY_LIB}" "//${FLD_3RDPARTY_LIB}" CPY)
    file(COPY ${CPY} DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_BUILD}/${BLD} PATTERN "*/${FLD_3RDPARTY_INC}/*" EXCLUDE)
endif()
# //资源
file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_SRC}/${FLD_CONFIGURATION})
file(GLOB FIL_CONFIGURATION ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_SRC}/${FLD_CONFIGURATION}/*)
if(FIL_CONFIGURATION)
    file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_SRC}/${FLD_CONFIGURATION} DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_BUILD}/${BLD})
endif()
file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_SRC}/${FLD_DATA})
file(GLOB FIL_DATA          ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_SRC}/${FLD_DATA}/*)
if(FIL_DATA)
    file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_SRC}/${FLD_DATA}          DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_BUILD}/${BLD})
endif()
file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_SRC}/${FLD_RESOURCES})
file(GLOB FIL_RESOURCES     ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_SRC}/${FLD_RESOURCES}/*)
if(FIL_RESOURCES)
    file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_SRC}/${FLD_RESOURCES}     DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/${FLD_BUILD}/${BLD})
endif()

if(DIR_3RDPARTY_INC)
    include_directories(${DIR_3RDPARTY_INC})
endif()
if(DIR_3RDPARTY_LIB)
    link_directories(${DIR_3RDPARTY_LIB})
endif()

# //HACK  Wl start end group
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--start-group")
link_directories("-Wl,--end-group")

# //编译目标
use_path_as_tar(${CMAKE_CURRENT_SOURCE_DIR} TAR)
clear_var(SRCS)
aux_source_directory(. SRCS)
add_executable(${PROJECT_NAME} ${SRCS})# add_library(${TAR} ${SRCS})

# //文件夹名称
# src

# //子目录
add_subdirectory(${FLD_SRC})# add_subdirectory(src)

# //子目录依赖库
target_link_libraries(${PROJECT_NAME} ${TAR}_${FLD_SRC})# target_link_libraries(${TAR} ${TAR}src) 

# //第三方依赖库
if(FIL_3RDPARTY_LIB)
    target_link_libraries(${PROJECT_NAME} ${FIL_3RDPARTY_LIB})
endif()
